# -*- coding: utf-8 -*-
"""
Created on Sun Sep 10 10:54:53 2023

@author: ZHOU
"""

import cv2
import threading
import mediapipe as mp
import math
import time

import pyautogui

pyautogui.FAILSAFE = True  # 启用自动防故障功能，左上角的坐标为（0，0），将鼠标移到屏幕的左上角，来抛出failSafeException异常


global q
q = 0
global kill_all_flag
kill_all_flag = 0

cap = cv2.VideoCapture(0)  # 开启摄像头
classifier = cv2.CascadeClassifier('./haarcascade_frontalface_alt2.xml')

global cam_img
ok, cam_img = cap.read()  # 读取摄像头图像
if ok is False:
    q = 1
    kill_all_flag = 1
    print('无法读取到摄像头！')
high=cam_img.shape[0]
width=cam_img.shape[1]
global left_point
global right_point
left_point = width/2+width*0.04
right_point = width/2-width*0.04

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False,max_num_hands=1,min_detection_confidence=0.6,min_tracking_confidence=0.75)

def Mouse(flag):
    print(flag)
    if flag==1:
#        pyautogui.moveTo(100, 100, duration=0.25)
        pass
    elif flag==2:
#        pyautogui.moveRel(-50, 0, duration=0.25)
        pass
def Keyborad(hand_jugg,gesture_str):
    print(hand_jugg,gesture_str)
    if hand_jugg=="Right Hand":
        if gesture_str=="1":
            pyautogui.click()
        elif gesture_str=="2":
            pyautogui.click(button='right')
        elif gesture_str=="4":
            pyautogui.mouseDown()
        elif gesture_str=="5":
            pyautogui.mouseUp()


    
def vector_2d_angle(v1,v2):
    '''
        求解二维向量的角度
    '''
    v1_x=v1[0]
    v1_y=v1[1]
    v2_x=v2[0]
    v2_y=v2[1]
    try:
        angle_= math.degrees(math.acos((v1_x*v2_x+v1_y*v2_y)/(((v1_x**2+v1_y**2)**0.5)*((v2_x**2+v2_y**2)**0.5))))
    except:
        angle_ =65535.
    if angle_ > 180.:
        angle_ = 65535.
    return angle_

def hand_angle(hand_):
    '''
        获取对应手相关向量的二维角度,根据角度确定手势
    '''
    angle_list = []
    #---------------------------- thumb 大拇指角度
    angle_ = vector_2d_angle(
        ((int(hand_[0][0])- int(hand_[2][0])),(int(hand_[0][1])-int(hand_[2][1]))),
        ((int(hand_[3][0])- int(hand_[4][0])),(int(hand_[3][1])- int(hand_[4][1])))
        )
    angle_list.append(angle_)
    #---------------------------- index 食指角度
    angle_ = vector_2d_angle(
        ((int(hand_[0][0])-int(hand_[6][0])),(int(hand_[0][1])- int(hand_[6][1]))),
        ((int(hand_[7][0])- int(hand_[8][0])),(int(hand_[7][1])- int(hand_[8][1])))
        )
    angle_list.append(angle_)
    #---------------------------- middle 中指角度
    angle_ = vector_2d_angle(
        ((int(hand_[0][0])- int(hand_[10][0])),(int(hand_[0][1])- int(hand_[10][1]))),
        ((int(hand_[11][0])- int(hand_[12][0])),(int(hand_[11][1])- int(hand_[12][1])))
        )
    angle_list.append(angle_)
    #---------------------------- ring 无名指角度
    angle_ = vector_2d_angle(
        ((int(hand_[0][0])- int(hand_[14][0])),(int(hand_[0][1])- int(hand_[14][1]))),
        ((int(hand_[15][0])- int(hand_[16][0])),(int(hand_[15][1])- int(hand_[16][1])))
        )
    angle_list.append(angle_)
    #---------------------------- pink 小拇指角度
    angle_ = vector_2d_angle(
        ((int(hand_[0][0])- int(hand_[18][0])),(int(hand_[0][1])- int(hand_[18][1]))),
        ((int(hand_[19][0])- int(hand_[20][0])),(int(hand_[19][1])- int(hand_[20][1])))
        )
    angle_list.append(angle_)
    
    return angle_list

def h_gesture(angle_list):
    '''
        # 二维约束的方法定义手势
        # fist five gun love one six three thumbup yeah
    '''
    thr_angle = 65.  #手指闭合则大于这个值（大拇指除外）
    thr_angle_thumb = 53.  #大拇指闭合则大于这个值
    thr_angle_s = 49.  #手指张开则小于这个值
    gesture_str = "Unknown"
    if 65535. not in angle_list:
        if (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "0"
        elif (angle_list[0]>thr_angle_thumb)  and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "1"
        elif (angle_list[0]>thr_angle_thumb)  and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "2"
        elif (angle_list[0]>thr_angle_thumb)  and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]>thr_angle):
            gesture_str = "3"
        elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
            gesture_str = "4"
        elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
            gesture_str = "5"
        elif (angle_list[0]<thr_angle_s)  and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
            gesture_str = "6"
        elif (angle_list[0]<thr_angle_s)  and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "8"
            
        elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
            gesture_str = "Pink Up"
        elif (angle_list[0]<thr_angle_s)  and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "Thumb Up"
        elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "Fuck"
        elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
            gesture_str = "Princess"
        elif (angle_list[0]<thr_angle_s)  and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
            gesture_str = "Bye"
        elif (angle_list[0]<thr_angle_s)  and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
            gesture_str = "Spider-Man"
        elif (angle_list[0]>thr_angle_thumb)  and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
            gesture_str = "Rock'n'Roll"
        
    return gesture_str

def hand_detect():
    global q
    global kill_all_flag
    global cam_img
    
    bye_flag = 0
    bye_time = time.time()
    
    hand_jugg = None
    gesture_str = None
    
    while True:
        time.sleep(0.1)
        while q==0:
            time.sleep(0.1)
            frame = cv2.cvtColor(cam_img, cv2.COLOR_BGR2RGB)
            results = hands.process(frame)
            if results.multi_handedness:                
                for hand_label in results.multi_handedness:
                    hand_jugg=str(hand_label).split('"')[1]+" Hand"
                    print(hand_jugg)
#                    cv2.putText(faceImg,hand_jugg,(50,200),0,1.3,(0,0,255),2)
                    
            if results.multi_hand_landmarks:
                for hand_landmarks in results.multi_hand_landmarks:
#                    mp_drawing.draw_landmarks(faceImg, hand_landmarks, mp_hands.HAND_CONNECTIONS)
                    hand_local = []
                    for i in range(21):
                        x = hand_landmarks.landmark[i].x*frame.shape[1]
                        y = hand_landmarks.landmark[i].y*frame.shape[0]
                        hand_local.append((x,y))
                    if hand_local:
                        angle_list = hand_angle(hand_local)
                        gesture_str = h_gesture(angle_list)
                        print(gesture_str)
#                        cv2.putText(faceImg,gesture_str,(50,100),0,1.3,(0,0,255),2)


            if gesture_str == "Bye":
                if bye_flag == 0:
                    bye_flag = 1
                elif bye_flag == 1 and time.time() - bye_time >= 3:
                    kill_all_flag = 1
                    q = 1
                    print("Good-Bye")
                else:
                    bye_flag = 1                    
            else:
                Keyborad(hand_jugg,gesture_str)
                bye_flag = 0
                
            hand_jugg = None
            gesture_str = None
            
            if q == 1:
                break
        if kill_all_flag == 1:
            break

    return 
                    
def track():
    global q
    global kill_all_flag
    global cam_img
        
    global left_point
    global right_point
    
    while True:
        time.sleep(0.1)
        while q==0:            
            time.sleep(0.05) 

            gray = cv2.cvtColor(cam_img,cv2.COLOR_BGR2GRAY)
            faceRects = classifier.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(32, 32))
            if len(faceRects):
                x,y,w,h = faceRects[0]
                # 框选出人脸   最后一个参数2是框线宽度 
#                cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)
                central_point = x+w/2 
                
                if central_point > left_point:
                    print("Right")
                    Mouse(1)
                elif central_point < right_point:
                    print("Left")
                    Mouse(2)
                else:
                    Mouse(0)
                
            if q == 1:
                print("S")

                break
        if kill_all_flag == 1:
            break

    return 

def img_main():
    global q
    global kill_all_flag
    global cam_img 
    
    thread_track = threading.Thread(target=track)
    thread_track.setDaemon(True)
    thread_track.start()
    
    thread_hand = threading.Thread(target=hand_detect)
    thread_hand.setDaemon(True)
    thread_hand.start()   
    
    
    while True:
        time.sleep(0.1)
        while q==0:
            cam_img = cv2.flip(cap.read()[1],1)

            cv2.imshow("video_feed",cam_img)
            # 展示图像            
            if q == 1:   # 通过esc键退出摄像
                q = 1

                print("暂停程序")
                cv2.destroyAllWindows()
                break
            if cv2.waitKey(10) == 27:
                kill_all_flag = 1
                q = 1

                print("结束程序")
                cv2.destroyAllWindows()
                break
        if kill_all_flag == 1:
            break
    cap.release()

    print("全部退出")
    return 

def main():
    img_main()    
    time.sleep(1)
    print("已退出所有程序")
    return 

if __name__ == "__main__":    
    main()
    
